﻿using System;
using System.Diagnostics;
using BaseWordStat;
using NetService.Parser;

namespace NetService
{
    public class TestNet
    {
        public Article SomeMethod()
        {
            try
            {
                var text = PageService.GetPage(@"http://www.newsweek.com/blogs/the-gaggle/2010/11/18/ahmed-ghailani-and-the-justice-department-s-communication-breakdown.html");

                if (string.IsNullOrEmpty(text))
                    return null;

                return GetArticleData(text);
            }
            catch (Exception e)
            {
                var myLog = new EventLog { Source = "WordStat" };

                myLog.WriteEntry(e.Message + "\n\n" + e.StackTrace, EventLogEntryType.Error);

            }

            return null;
        }

        private Article GetArticleData(string text)
        {
            try
            {
                var article = new Article();

                article.Author = HtmlParser.GetAuthor(text);

                if (string.IsNullOrEmpty(article.Author))
                    return null;

                article.Date = HtmlParser.GetDate(text);

                if (string.IsNullOrEmpty(article.DateStr))
                    return null;

                var url = HtmlParser.GetUrl(text);

                if (string.IsNullOrEmpty(url))
                    return null;

                var title = HtmlParser.GetTitle(text);

                if (string.IsNullOrEmpty(title))
                    return null;


                article.Text = HtmlParser.GetArticle(text).Trim();

                if (string.IsNullOrEmpty(article.Text))
                    return null;

                article.Words = HtmlParser.GetWords(article.Text);

                if (article.Words.Count == 0)
                    return null;

                return article;

            }
            catch (Exception e)
            {
                var myLog = new EventLog { Source = "WordStat" };

                myLog.WriteEntry(e.Message + "\n\n" + e.StackTrace, EventLogEntryType.Error);
            }

            return null;
        }
    }
}
